<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Hash;
use DB;

class UserController extends Controller
{
    //
    public function getAdd()
    {
    	// dd(22);
    	return view('admin.user.add');
    }
    public function postInsert(Request $request)
    {
    	// dd(222);
    	//表单验证
    	$this->validate($request,[
    		
    		'username' =>'required|regex:/^\w{4,20}$/|unique:users,username',
    		'password'=>"required|same:repassword",
    		],[
    			'username.required'=>'用户名不能为空',
    			'username.regex'=>'格式不正确',
    			'username.unique'=>'用户名已经存在',
    			'password.same'=>'两次密码不一样!!',
    		]);



    	$data = $request->except(['_token','repassword']);
    	//加密密码
    	$data['password'] = Hash::make($data['password']);
    	// dd($data);
    	//文件上传
       // $data['profile'] = $this->upload($request);

        if($request->hasFile('profile')){


        $data['profile'] = $this->upload($request);
        }


    	// if($request->hasFile('profile')){
    	// 	$fileName = time().rand(100000,999999);
    	// 	$suffix = $request->file('profile')->getClientOriginalExtension();
    	// 	// dd($suffix);
    	// 	$dir = './uploads'.date('Ymd');

    	// 	$request->file('profile')->move($dir,$fileName.'.'.$suffix);
    	// 	//拼接路径
    	// 	$data['profile'] = trim($dir.'/'.$fileName.'.'.$suffix,'.');
    	// }
    	// dd($data);



    	//写入数据库
    	$res = DB::table('users')->insert($data);
    	if($res){
    		// echo 'ok';
            return redirect('/user/list')->with('info','添加成功');
    	}else{
    		// echo 'error';
            return back()->with('info','添加失败');
    	}
    }
    public function getList(Request $request)
    {
        $keyword = $request->input('keyword');
        if(empty($keyword)){


        // dd(222);
        $user = DB::table('users')->orderBy('id','desc')->paginate($request->input('num',10));
        }else{
            $user = DB::table('users')
            ->where('username','like','%'.$keyword.'%')
            ->paginate($request->input('num',10));
        }
        // dd($user);
        return view('admin.user.list',['user'=>$user,'request'=>$request]);
    }

    public function getEdit(Request $request)
    {
        $id = $request->input('id');
        //读取数据库
        $user = DB::table('users')->where('id','=',$id)->first();
        // dd($user);
        return view('admin.user.edit',['user'=>$user]);
    }

    //更新操作
    public function postUpdate(Request $request)
    {
        // dd($request->all());
        $data = $request->except(['_token','profile','id']);
        //处理图片
        if($request->hasFile('profile')){


        $data['profile'] = $this->upload($request);
        }

        $res = DB::table('users')->where('id',$request->input('id'))->update($data);
        if($res){
            // echo 'ok';
            return redirect('/user/list')->with('info','更新成功');
        }else{
            // echo 'fail';
            return back()->with('info','更新失败');
        }
   }

   public function upload($request)
   {
        if($request->hasFile('profile')){
            $fileName = time().rand(100000,999999);
            $suffix = $request->file('profile')->getClientOriginalExtension();
            // dd($suffix);
            $dir = './uploads'.date('Ymd');

            $request->file('profile')->move($dir,$fileName.'.'.$suffix);
            //拼接路径
            $data['profile'] = trim($dir.'/'.$fileName.'.'.$suffix,'.');
            return $data['profile'];
        }
        return null;
   }

   public function getDelete(Request $request)
   {
        $id = $request->input('id');
        //删除图片
        $info = DB::table('users')->where('id',$id)->first();
        @unlink('.'.$info->profile);
        $res = DB::table('users')->where('id',$id)->delete();
        if($res){
            // echo 'ok';
            return redirect('/user/list')->with('info','删除成功');
        }else{
            // echo 'fail';
            return back()->with('info','删除失败');
        }
   }


}
